﻿CREATE TRIGGER [TRG_Account_NameCheck]
    ON [dbo].[tblAccount]
    FOR INSERT, UPDATE 
    AS 
    BEGIN
    	if exists ( select 1 from tblAccount T1 join inserted T2 on T1.Name = T2.Name and coalesce(T1.OwnerID, -1) = coalesce(T2.OwnerID, -1) and T1.AccountID <> T2.AccountID )
			raiserror( 'Account names must be unique', 18, -1 )
    END